package com.wonyesoft.wuliu.acticity.user;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javabeen.LinkManBeen;

import httptools.HttpJsonTool;
import View.InPutDialog;
import View.InPutDialog.getData;
import View.InPutDialog.getUserIntent;
import adapter.LinkManAdapter;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.reflect.TypeToken;
import com.wonyesoft.wuliu.AppContent;
import com.wonyesoft.wuliu.R;
import com.wonyesoft.wuliu.acticity.base.BaseActivity;
import com.wonyesoft.wuliu.acticity.order.AddCargoOrderActivity;
import com.wonyesoft.wuliu.api.GagApi;

public class UserLinkMan_Activity extends BaseActivity {

	private ListView linkManView;
	private AppContent app;
	private LinkManAdapter adapter;

	EditText driverPhone;
	EditText driverName;
	Dialog alertdialog;
	private List<LinkManBeen> linkManBeens = new ArrayList<LinkManBeen>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user_linkman);
		iniId();
		getLinkManInfo();
	}

	private void iniId() {
		TextView head_title = (TextView) findViewById(R.id.head_title);
		head_title.setText("常用联系人");
		ImageView imageView=(ImageView) findViewById(R.id.head_left);
		imageView.setVisibility(View.VISIBLE);
		imageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				UserLinkMan_Activity.this.finish();
			}
		});
		linkManView = (ListView) findViewById(R.id.linkManView);
		app = (AppContent) getApplication();
		app.addActivity(this);
		Button addLinkMan = (Button) findViewById(R.id.addLinkMan);
		addLinkMan.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				showAddDialog();
			}
		});
	}

	private void getLinkManInfo() {
		Type type = new TypeToken<List<LinkManBeen>>() {
		}.getType();
		HttpJsonTool httpJsonTool = new HttpJsonTool(this);
		HashMap<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("tcargoownerid", app.userId);
		httpJsonTool.SetOnLister(new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if (msg.what == 1) {
					List<LinkManBeen> data = (List<LinkManBeen>) msg.obj;
					for (int i = 0; i < data.size(); i++) {
						linkManBeens.add(data.get(i));
						iniListView();
					}
				}
			}
		});
		httpJsonTool.StartWork(dataMap, GagApi.GET_LINK_MAN, type, null);

	}

	private void iniListView() {
		if (linkManBeens.size() <= 0) {
			return;
		}
		if (adapter != null) {
			adapter.notifyDataSetChanged();
		} else {
			adapter = new LinkManAdapter(this, linkManBeens,
					R.layout.user_link_man_iteam);
			linkManView.setAdapter(adapter);
			linkManView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(UserLinkMan_Activity.this,
							AddCargoOrderActivity.class);
					intent.putExtra("linkman", linkManBeens.get(arg2));
					Log.v("linkManBeens.get(arg2)",linkManBeens.get(arg2).getCarownername());
					UserLinkMan_Activity.this.setResult(5201, intent);
					UserLinkMan_Activity.this.finish();
				}
			});
			linkManView.setOnItemLongClickListener(new OnItemLongClickListener() {

				@Override
				public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub
//					removeLinkMan(arg2);
					showUserCheckDialog(arg2);
					return false;
				}
			});
		}
	}
	
	private void showUserCheckDialog(final int poison){
		InPutDialog inPutDialog=new InPutDialog(this, "提示", "是否删除该用户?",null,null);
		inPutDialog.setGetUserIntent(new getUserIntent() {

			@Override
			public void getUserIntentInfo() {
				// TODO Auto-generated method stub
				removeLinkMan(poison);
			}
		});
		inPutDialog.getCheckDialog(false);
	}
	
	
	private void removeLinkMan(int poison){
		HashMap< String, Object> data=new HashMap<String, Object>();
		data.put("carownerid", linkManBeens.get(poison).getCarownerid());
		//data.put("", value);
//		data.put(key, value);
		HttpJsonTool httpJsonTool=new HttpJsonTool(this);
		httpJsonTool.SetOnLister(new Handler(){
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if (msg.what==1) {
					Toast.makeText(UserLinkMan_Activity.this, (String)msg.obj, Toast.LENGTH_LONG).show();
					getLinkManInfo();
				}
			}
		});
		httpJsonTool.StartWork(data, GagApi.REMOVE_LINK_MAN, null, null);
	}
	

	private void showAddDialog() {

		LinearLayout dateTimeLayout = (LinearLayout) this.getLayoutInflater()
				.inflate(R.layout.input_dialog_view, null);
		alertdialog = new AlertDialog.Builder(this).create();
		// alertdialog.set
		alertdialog.show();
		alertdialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); 
		alertdialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
		alertdialog.setContentView(dateTimeLayout);
		TextView title = (TextView) dateTimeLayout.findViewById(R.id.tit);
		title.setText("请输入");
		Button check = (Button) dateTimeLayout.findViewById(R.id.sure);
		Button giveup = (Button) dateTimeLayout.findViewById(R.id.close);
		driverPhone = (EditText) dateTimeLayout.findViewById(R.id.driverPhone);
		driverName = (EditText) dateTimeLayout.findViewById(R.id.driverName);
		check.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String p = driverPhone.getText().toString();
				String n = driverName.getText().toString();
				if (p.length() > 0) {
					if (n.length() > 0) {
						addDriver(p, n);
						Log.v("p, n", p+"+"+n);
						alertdialog.dismiss();
					} else {
						Toast.makeText(UserLinkMan_Activity.this, "请输入司机名字",
								Toast.LENGTH_LONG).show();
					}
				} else {
					Toast.makeText(UserLinkMan_Activity.this, "请输入司机电话号码",
							Toast.LENGTH_LONG).show();
				}

			}
		});
		giveup.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// inputDate.setText("点击选择时间");
				alertdialog.dismiss();
			}
		});
	}

	private void addDriver(String driverPhone, String driverName) {
		// TODO Auto-generated method stub
		HashMap<String, Object> data = new HashMap<String, Object>();
		data.put("phone", driverPhone);
		data.put("carownername", driverName);
		data.put("tcargoownerid", app.userId);
		HttpJsonTool httpJsonTool = new HttpJsonTool(this);
		httpJsonTool.SetOnLister(new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if (msg.what == 1) {
					getLinkManInfo();
				}else {
					Toast.makeText(UserLinkMan_Activity.this, (String)msg.obj, Toast.LENGTH_LONG).show();
				}
			}
		});
		httpJsonTool.StartWork(data, GagApi.ADD_LINK_MAN, null, null);
	}

}
